package com.jic.member.controller.power;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.response.manager.product.GoodsSpuPageQueryResponse;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MepowerProduct;
import com.jic.member.request.power.MepowerProductRequest;
import com.jic.member.response.power.MeproductResponse;
import com.jic.member.service.power.MepowerProductService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@ApiOperation(value = "权益商品关联controller")
@RestController
@RequestMapping("/memberPowerProduct")
@Slf4j
public class MepowerProductController {

    @Resource
    private MepowerProductService mepowerProductService;


    /*
     * 分页查询
     * */
    @ApiOperation(value = "根据权益id查询当前店铺下所有商品列表")
    @ResponseBody
    @RequestMapping("/querySpuList")
    public RestResult<GoodsSpuPageQueryResponse> queryPowerStoreList(@RequestBody MepowerProductRequest form) {
        log.info("开始进入当前店铺下所有商品列表=========》,{}", form.toString());
        RestResult queryPowerStoreList = mepowerProductService.querySpuPage(form);
        return queryPowerStoreList;
    }

    /**
     * 活动批量添加商品
     * @param request
     * @return
     */
    @PostMapping("saveProduct")
    @ApiOperation(value = "批量添加商品")
    public RestResult saveProduct(@RequestBody MepowerProductRequest request) {
        return RestResult.success(mepowerProductService.saveProduct(request));
    }

    /*
     * 分页查询
     * */
    @ApiOperation(value = "权益商品所有列表")
    @ResponseBody
    @RequestMapping("/querySpuListForTable")
    public RestResult<PageResult<MeproductResponse>> querySpuList(@RequestBody MepowerProductRequest form) {
        log.info("开始进入权益商品所有列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MepowerProduct memberRequest = BeanUtil.copy(form, MepowerProduct.class);
        PageResult<MepowerProduct> result = mepowerProductService.queryPage(memberRequest, page);
        List<MeproductResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MeproductResponse memberResponse = BeanUtil.copy(temp, MeproductResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MeproductResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /*
     * 删除权益商品
     * */
    @ApiOperation(value = "删除权益商品")
    @ResponseBody
    @RequestMapping("/deletePowerProducts")
    public RestResult deletePowerProduct(@RequestBody MepowerProductRequest form){
        mepowerProductService.deletePowerProduct(form);
        return RestResult.success(1);
    }

    /*
     * 批量删除权益商品
     * */
    @ApiOperation(value = "批量删除权益商品")
    @ResponseBody
    @RequestMapping("/deletePowerProductsByBatch")
    public RestResult deletePowerProductsByBatch(@RequestBody MepowerProductRequest form){
        mepowerProductService.deletePowerProductsByBatch(form);
        return RestResult.success(1);
    }

    /**
     * 添加全部商品
     * @param request
     * @return
     */
    @PostMapping("saveAllProductInfo")
    @ApiOperation(value = "添加全部商品")
    public RestResult addAllProduct(@RequestBody MepowerProductRequest request) {
        return RestResult.success(mepowerProductService.addAllProduct(request));
    }
}
